
/**
 * 病种筛查配置管理初始化
 */
var SdcTreatPlan = {
    id: "SdcTreatPlanTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdcTreatPlan.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '方案内容', field: 'itemName', align: 'center', width:150, valign: 'middle', sortable: false},
        {title: '方案备注', field: 'itemDesc', align: 'center', width:200,valign: 'middle', sortable: false},
        {title: '文献来源', field: 'sourceFlag', align: 'center', width:80, valign: 'middle', sortable: false, formatter:function(value,row,index) {
                if (value == 1) {
                    return "有";
                } else {
                    return "无";
                }
            }},
        {title: '关联质控项', field: 'qcDesc', align: 'center', width:200, valign: 'middle', sortable: false},
        {title: '是否CDSS方案', field: 'isCdss', align: 'center', width:120,valign: 'middle', sortable: false, formatter:function(value,row,index) {
                if (value == 1) {
                    return "是";
                } else {
                    return "否";
                }
            }},
        {title: 'CDSS前置条件', field: 'noCondition', align: 'center', width:120,valign: 'middle', sortable: false, formatter:function(value,row,index) {
                if (value == 1) {
                    return "有";
                } else {
                    return "无";
                }
            }},
        /*{title: '开始时间', field: 'pushStart', align: 'center', width:100,valign: 'middle', sortable: false},
        {title: '结束时间', field: 'pushEnd', align: 'center', width:100,valign: 'middle', sortable: false},
        {title: '计算单位', field: 'timeUnit', align: 'center', width:100,valign: 'middle', sortable: false, formatter:function(value,row,index) {
                if (value == 1) {
                    return "年";
                } else if (value == 2) {
                    return "月";
                }else if (value == 3) {
                    return "日";
                }else if (value == 4) {
                    return "时";
                }else if (value == 5) {
                    return "分";
                }else if (value == 6) {
                    return "秒";
                } else {
                    return "";
                }
            }},*/
        {title: '显示时间段', field: 'referenceTime', align: 'center', width:180,valign: 'middle', sortable: false},
        {title: '显示前提', field: 'showPremise', align: 'center', width:180,valign: 'middle', sortable: false},
        {title: '是否有效', field: 'isActive', align: 'center', width:90,valign: 'middle', sortable: false,
            formatter:function(value,row,index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }
        },
        {title: '操作', field: 'id', align: 'center', width: '120px', valign: 'middle',formatter:function(value,row,index){
                var _html = '<a href="javascript:SdcTreatPlan.edit(\''+row.id+'\')" title="修改"><i class="hisui-icon icon-write-order"></i></a>';
                return _html;
            }}
    ];
};

/**
 * 检查是否选中
 */
SdcTreatPlan.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdcTreatPlan.seItem = selected[0];
        return true;
    }
};

SdcTreatPlan.add = function () {
    var index = Sys.open('新增诊疗方案',Sys.backendPath + '/sdc/sdctreatplan/to_add',['900px', '450px']);
    this.layerIndex = index;
};

SdcTreatPlan.edit = function (id) {
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
    if (flag) {
        var recommendType = $('#recommendType').combobox('getValue');
        var entityId = $('#QCDic').combobox('getValue');
        //var index = Sys.open('诊疗方案维护详情',Sys.backendPath + '/sdc/sdctreatplan/to_update/' + id +"/"+ recommendType +"/" + entityId,['800px', '420px']);
            var index = Sys.open('诊疗方案维护详情',Sys.backendPath + '/sdc/sdctreatplan/to_update?id=' + id + '&recommendType=' + recommendType + '&entityId=' + entityId,['900px', '450px']);
        this.layerIndex = index;
    }
};

SdcTreatPlan.del = function (id,name) {
    var recommendType = $('#recommendType').combobox('getText');
    var flag = false;
    if(id){
        flag = true;
    }else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
    if (flag) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sdc/sdctreatplan/delete", function (data) {
                Sys.success("删除成功!");
                SdcTreatPlan.table.refresh();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("sdcTreatPlanId",id);
            ajax.set("recommendType",recommendType);
            ajax.start();
        };
        Sys.confirm("是否删除诊疗方案?",operation);
    }
};


/**
 * 查询病种筛查配置列表
 */
SdcTreatPlan.search = function () {
    var queryData = {};
    queryData['qcentityId'] = $('#QCDic').combobox('getValue');
    queryData['recommendType'] = $('#recommendType').combobox('getText');
    SdcTreatPlan.table.refresh({query: queryData, pageNumber: 1});
    SdcTreatPlan.initState();
};

SdcTreatPlan.manual = function(){
    var ajax = new $ax("/api/updateEntityRecommendData", function (data) {
        $.messager.popover({msg: '操作成功！',type:'success',timeout: 1000,style:{top:($(window).height()-50)/2+$(document).scrollTop(),left:($(window).width()-140)/2+$(document).scrollLeft()}})
    }, function (data) {
        $.messager.alert("错误提示", "状态更新错误!", 'info');
    });
    ajax.set('entityId', $("#QCDic").combobox('getValue'));
    ajax.start();
}

SdcTreatPlan.log = function(){
    var qcEntity = $("#QCDic").combobox("getValue");
    var index = Sys.open_c('CDSS推荐数据初始化日志记录', '/api/initCdssRecommendData/log?qcEntity=' + qcEntity, ['1000px', '500px'], function () {
    });
    this.layerIndex = index;
}

SdcTreatPlan.offLine = function (){
    var entityId = $("#QCDic").combobox("getValue");
    var ajax = new $ax(Sys.backendPath + "/sdc/sdctreatplan/offLine", function (data) {
        Sys.success("切换成功!");
        SdcTreatPlan.initState();
    }, function (data) {
        Sys.error("切换失败!" + data.responseJSON.message + "!");
    });
    ajax.set("entityId",entityId);
    ajax.start();
};

SdcTreatPlan.initState = function (){
    var entityId = $("#QCDic").combobox("getValue");
    var ajax = new $ax(Sys.backendPath + "/sdc/sdctreatplan/loadLine", function (data) {
        if (data.planActive == 1){
            $("#online").hide();
            $("#offline").show();
            $("#planActive").val("当前状态：（已上线）");
        }
        else {
            $("#offline").hide();
            $("#online").show();
            $("#planActive").val("当前状态：（未上线）");
        }
    }, function (data) {
    });
    ajax.set("entityId",entityId);
    ajax.start();
};

SdcTreatPlan.initRecommend = function () {
    var data = $.parseJSON($("#recommendTypeMap").val());
    var keys = [];
    for (var key in data){
        keys.push(key);
    }
    $('#recommendType').combobox("setValue", keys[1]);
    $('#recommendType').combobox("setText", data[keys[1]]);
};

$(function () {

    /*$HUI.combobox('#recommendType',
        {
            data:$.parseJSON($("#recommendTypeMap").val()),
            valueField:'value',
            textField:'text',
            defaultFilter: 6,
            onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
                var val = $(this).combobox("getData");
                $(this).combobox("setValue", val[1].value);
            }
        })*/



    $HUI.combobox('#QCDic',
        {
            data:$.parseJSON($("#entityMap").val()),
            valueField:'value',
            textField:'text',
            defaultFilter: 6,
            onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
                var val = $(this).combobox("getData");
                $(this).combobox("setValue", val[1].value);
            }
        })
    SdcTreatPlan.initState();

    var entityType = $('#QCDic').combobox('getValue');
    var recommendType = $('#recommendType').combobox('getText');

    //项目规则获取
    var defaultColunms = SdcTreatPlan.initColumn();
    var table = new BSTable(SdcTreatPlan.id, Sys.backendPath + "/sdc/sdctreatplan/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(10);
    table.setSubHeight(-40);
    table.setShowExport(false);
    table.setShowRefresh(false);
    table.setShowToggle(false);
    table.setShowColumns(false);
    table.setQueryParams({qcentityId:entityType,recommendType:recommendType});
    SdcTreatPlan.table = table.init();
});
